package com.example.icongyou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.icongyou.R;
import com.example.icongyou.bean.TaskHeatBean;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务热度适配器：通用适配查看热度、讨论热度
 */
public class TaskHeatAdapter extends RecyclerView.Adapter<TaskHeatAdapter.HeatViewHolder> {
    private Context context;
    private List<TaskHeatBean.HeatItemBean> heatList = new ArrayList<>();
    private String countLabel; // 统计标签（如"查看次数"、"讨论次数"）

    // 构造方法：传入统计标签
    public TaskHeatAdapter(Context context, String countLabel) {
        this.context = context;
        this.countLabel = countLabel;
    }

    // 设置热度数据（外部调用更新）
    public void setHeatList(List<TaskHeatBean.HeatItemBean> heatList) {
        this.heatList = heatList != null ? heatList : new ArrayList<>();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HeatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载热度项布局
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.item_task_heat, parent, false);
        return new HeatViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HeatViewHolder holder, int position) {
        TaskHeatBean.HeatItemBean heatItem = heatList.get(position);

        // 绑定数据：排名、任务名称、统计次数
        holder.tvRank.setText(heatItem.getRank() + ".");
        holder.tvTaskName.setText(heatItem.getTaskName());
        holder.tvCount.setText(countLabel + "：" + heatItem.getCount());
    }

    @Override
    public int getItemCount() {
        return heatList.size();
    }

    /**
     * ViewHolder：缓存热度项控件
     */
    static class HeatViewHolder extends RecyclerView.ViewHolder {
        TextView tvRank;
        TextView tvTaskName;
        TextView tvCount;

        public HeatViewHolder(@NonNull View itemView) {
            super(itemView);
            // 绑定布局控件（与 item_task_heat.xml 中 ID 一致）
            tvRank = itemView.findViewById(R.id.tv_rank);
            tvTaskName = itemView.findViewById(R.id.tv_task_name);
            tvCount = itemView.findViewById(R.id.tv_count);
        }
    }
}